<template>
    <div class="classify-edit">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="500px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                ref="formRef"
                :model="formData"
                :rules="formRules"
                label-width="80px"
            >
                <el-form-item label="分类名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入分类名称"
                        maxlength="50"
                        show-word-limit
                    />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number
                        v-model="formData.sort"
                        :min="0"
                        :max="9999"
                        placeholder="请输入排序值"
                        class="w-full"
                    />
                </el-form-item>
                <el-form-item label="状态" prop="is_show">
                    <el-radio-group v-model="formData.is_show">
                        <el-radio :label="1">显示</el-radio>
                        <el-radio :label="0">隐藏</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import { apiShopCategoryAdd, apiShopCategoryEdit, apiShopCategoryDetail } from '@/api/shop'
import feedback from '@/utils/feedback'
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'

interface Props {
    dictData?: Record<string, any[]>
}

interface Emits {
    (event: 'success'): void
    (event: 'close'): void
}

defineProps<Props>()
const emit = defineEmits<Emits>()

const popupRef = shallowRef<InstanceType<typeof Popup>>()
const formRef = shallowRef<FormInstance>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value === 'edit' ? '编辑分类' : '新增分类'
})

const formData = ref<{
    id?: number,
    name: string,
    sort: number,
    is_show: number,
}>({
    name: '',
    sort: 0,
    is_show: 1,
})

const formRules = {
    name: [
        {
            required: true,
            message: '请输入分类名称',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入排序值',
            trigger: ['blur']
        }
    ],
    is_show: [
        {
            required: true,
            message: '请选择显示状态',
            trigger: ['change']
        }
    ]
}

// 获取分类详情
const getDetail = async (id: number) => {
    const data = await apiShopCategoryDetail({ id })
    Object.assign(formData, data)
}

// 提交表单
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData.value }
    data.is_show = Number(data.is_show)
    if (mode.value === 'edit') {
        await apiShopCategoryEdit(data)
        feedback.msgSuccess('编辑成功')
    } else {
        await apiShopCategoryAdd(data)
        feedback.msgSuccess('添加成功')
    }
    resetForm()
    popupRef.value?.close()
    emit('success')
}

// 关闭弹窗
const handleClose = () => {
    emit('close')
}

// 重置表单
const resetForm = () => {
    Object.assign(formData.value, {
        name: '',
        sort: 0,
        is_show: 1,
    })
}

// 打开弹窗
const open = (type = 'add', row?: any) => {
    mode.value = type
    resetForm()
    popupRef.value?.open()
    formRef.value?.resetFields()
    if (type === 'edit' && row) {
        formData.value.id = row.id
        getDetail(row.id)
    }
}

defineExpose({
    open
})
</script>

<style scoped>
.classify-edit {
    :deep(.el-form-item__label) {
        font-weight: 500;
    }
}
</style>